package com.sgb.knowledgeshare.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.knowledgeshare.BR;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.entity.TabelListEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import java.util.List;

/**
 * Created by：冯齐 on 2021/1/28 16:36
 * Description： 视频发布标签的adapter
 */
public class LabelAdapter extends BaseRecycleAdapter<TabelListEntity> {
    private boolean delete ;
    public LabelAdapter(Context context, List<TabelListEntity> datas) {
        super(context, R.layout.item_label, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final TabelListEntity entity) {
        holder.setBinding(BR.viewModel, null);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
       TextView labelName = holder.getItem().findViewById(R.id.label_name);
       RelativeLayout rllabel = holder.getItem().findViewById(R.id.rl_label);
        TextView labelName2 = holder.getItem().findViewById(R.id.label_name2);
        ImageView labelDelete = holder.getItem().findViewById(R.id.label_delete);
        //处理第一个是添加标签和标签的处理方式
        if (position == 0 && datas.get(position).getLabelNo().equals("add")) {
            if (delete){
                rllabel.setVisibility(View.GONE);
            }else {
                rllabel.setVisibility(View.VISIBLE);
                labelName.setVisibility(View.GONE);
                labelName2.setVisibility(View.VISIBLE);
                labelDelete.setVisibility(View.GONE);
            }
        } else {
            rllabel.setVisibility(View.VISIBLE);
            labelName.setVisibility(View.VISIBLE);
            labelName2.setVisibility(View.GONE);
            if (delete){
                labelDelete.setVisibility(View.VISIBLE);
            }else {
                labelDelete.setVisibility(View.GONE);
            }
            labelName.setText(datas.get(position).getLabelName()+"");
            labelName.setSelected(datas.get(position).isSelected());
        }
        //长按删除事件
        labelName.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                delete = true;
                //长按删除，完成按钮显示
                callback.ShowView();
                notifyDataSetChanged();
                return false;
            }
        });
        //添加点击事件
        labelName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onClick(position);
            }
        });
        //添加标签数据信息
        labelName2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.addLabel();
            }
        });
        //点击删除事件
        labelDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           //     callback.LabelDelete();
                delLabel(position);
            }
        });

    }

    private OnIsDelete callback;
    public void setCallback(OnIsDelete callback) {
        this.callback = callback;
    }

    //点击完成取消删除
    public void setAdapterView() {
        delete = false;
        notifyDataSetChanged();
    }


    public interface OnIsDelete {
        void ShowView();

        void onClick(int position);

        void addLabel();

        void delect();
    }

    public void addLabel(List<TabelListEntity> data){
        TabelListEntity tabellistentity = new TabelListEntity();
        tabellistentity.setLabelName("＋ 添加标签");
        tabellistentity.setLabelNo("add");
        if (data.size() >= 5) {
            getDatas().addAll(data);
        }else {
            getDatas().addAll(data);
            getDatas().add(0, tabellistentity);
        }
        notifyDataSetChanged();
    }

    public void delLabel(int position){
        callback.delect();
        TabelListEntity tabellistentity = new TabelListEntity();
        tabellistentity.setLabelName("＋ 添加标签");
        tabellistentity.setLabelNo("add");
        getDatas().remove(position);
        int i = getDatas().indexOf(tabellistentity);
        if(i == - 1){
            getDatas().add(0, tabellistentity);
        }
        notifyDataSetChanged();
    }

}
